<?php

/**
 * This is the model class for table "user_quiz_attempt_details".
 *
 * The followings are the available columns in table 'user_quiz_attempt_details':
 * @property integer $id
 * @property integer $attempt_id
 * @property integer $question_id
 * @property integer $option_id
 * @property integer $order
 * @property string $answer_status
 * @property integer $points
 * @property string $created
 * @property string $modified
 */
class UserQuizAttemptDetails extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserQuizAttemptDetails the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_quiz_attempt_details';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('attempt_id, question_id,answer_status', 'required'),
			array('attempt_id, question_id, option_id, points', 'numerical', 'integerOnly'=>true),
			array('answer_status', 'length', 'max'=>9),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('modified','default','value'=>new CDbExpression('NOW()'),'setOnEmpty'=>false,'on'=>'update'),
			array('created,modified','default','value'=>new CDbExpression('NOW()'),'setOnEmpty'=>false,'on'=>'insert'),
			array('id, attempt_id, question_id, option_id, answer_status, points, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'attempt_id' => 'Attempt',
			'question_id' => 'Question',
			'option_id' => 'Option',
			'answer_status' => 'Answer Status',
			'points' => 'Points',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('attempt_id',$this->attempt_id);
		$criteria->compare('question_id',$this->question_id);
		$criteria->compare('option_id',$this->option_id);
		$criteria->compare('answer_status',$this->answer_status,true);
		$criteria->compare('points',$this->points);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	//get list of model objects of specific type
	public static function getList($type, $arrParams=array())
	{
		switch ($type)
		{
			case "ATTEMPT-ID":
				$attempt = UserQuizAttemptDetails::model()->findAll(
					'attempt_id=?',array($arrParams['attemptid'])
					);
				return $attempt;
				break;
			case "ANSWER-STATUS":
				$attempt = UserQuizAttemptDetails::model()->findAll(
					'attempt_id=? and answer_status=?',array($arrParams['attemptid'],$arrParams['status'])
					);
				return $attempt;
			break;
		} 
	}
		//get  model object of specific type
	public function getModel($type, $arrParams) 
	{
		switch($type)
		{
			
			case "ATTEMPT-QUIZ-ID":
				$attempt = UserQuizAttemptDetails::model()->find(
					'attempt_id=? and question_id=?',array($arrParams['attemptid'],$arrParams['questionid'])
					);

				return $attempt;
			break;
			case "ATTEMPT-ORDER-ID":
				$attempt = UserQuizAttemptDetails::model()->find(
					'attempt_id=? and `order`=?',array($arrParams['attemptid'],$arrParams['order'])
					);

				return $attempt;
			break;
			case "ANSWER-STATUS":
				$attempt = UserQuizAttemptDetails::model()->find(
					'attempt_id=? and answer_status=?',array($arrParams['attemptid'],$arrParams['status'])
					);
				return $attempt;
			break;
			
	
		
		}
	}
	public function getMaxOrderByAttemptId($attemptid)
	{
		
		$attemptid= UserQuizAttemptDetails::model()->findAll(
					'attempt_id=? ORDER BY `order` DESC LIMIT ?',array((int)$attemptid,1)
					);
		
		
		if ($attemptid)
		{
			return $attemptid[0]->order;
		}
		else
		return false;
	}
}